package cn.shyouchi.wuguanjia.android.ui.statistic;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.AbseUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.EarlyUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.LateUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.OutUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.PracticeUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.SecondedUser;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class StatisticMonthSignDetailActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_statistic_list)
    RecyclerView mRvStatisticList;

    List<MultiItemEntity> mMultiItemEntities = new ArrayList<>();
    StatisticMonthSignDetailAdapter mSignDetailAdapter;


    AttendanceApi mAttendanceApi;

    String date;

    boolean isYear;
    String projectid;


    int type = StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_LATE;

    @Override
    public int initContentView() {
        return R.layout.activity_statistic_month_sign_detail;
    }

    @Override
    public void initInjector() {
        mAttendanceApi = new AttendanceApi(mContext);
        date = getIntent().getStringExtra("date");
        type = getIntent().getIntExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_LATE);
        isYear = getIntent().getBooleanExtra("year", false);

        projectid = getIntent().getStringExtra("projectid");

    }

    @Override
    public void initUiAndListener() {

        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

//        for (int i = 0; i < 30; i++) {
//            mMultiItemEntities.add(new LateUser());
//        }

        mSignDetailAdapter = new StatisticMonthSignDetailAdapter(mMultiItemEntities);
        mRvStatisticList.setAdapter(mSignDetailAdapter);
        mRvStatisticList.setLayoutManager(new LinearLayoutManager(mContext));
        mSignDetailAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.empty_view_layout, null));


        fetchData(type);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchData(int type) {
        switch (type) {
            case StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_LATE:
                mTopBar.setTitle("迟到用户列表");
                fetchLateUser();
                break;
            case StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_EARLY:
                mTopBar.setTitle("早退用户列表");
                getMothEarlyUsers();
                break;
            case StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_ABSE:
                mTopBar.setTitle("旷工用户列表");
                getMothAbseUsers();
                break;
            case StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_OUT:
                mTopBar.setTitle("外出用户列表");
                getMothOutUserUsers();
                break;
            case StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_PRAC:
                mTopBar.setTitle("实习用户列表");
                getMothPracticeUserUsers();
                break;
            case StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_SECO:
                mTopBar.setTitle("借调用户列表");
                getMothSecondedUserUsers();
                break;

        }
    }


    private void fetchLateUser() {
        mAttendanceApi.getMothLateUsers(date, isYear,projectid)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseRequestResult<List<LateUser>>, List<LateUser>>() {
                    @Override
                    public List<LateUser> call(BaseRequestResult<List<LateUser>> result) {
                        return result.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<LateUser>>(mContext) {
                    @Override
                    public void onNext(List<LateUser> users) {
                        mMultiItemEntities.clear();
                        mMultiItemEntities.addAll(users);
                        mSignDetailAdapter.notifyDataSetChanged();
                    }
                });
    }


    private void getMothEarlyUsers() {
        mAttendanceApi.getMothEarlyUsers(date, isYear,projectid)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseRequestResult<List<EarlyUser>>, List<EarlyUser>>() {
                    @Override
                    public List<EarlyUser> call(BaseRequestResult<List<EarlyUser>> result) {
                        return result.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<EarlyUser>>(mContext) {
                    @Override
                    public void onNext(List<EarlyUser> users) {
                        mMultiItemEntities.clear();
                        mMultiItemEntities.addAll(users);
                        mSignDetailAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void getMothAbseUsers() {
        mAttendanceApi.getMothAbseUserUsers(date, isYear,projectid)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseRequestResult<List<AbseUser>>, List<AbseUser>>() {
                    @Override
                    public List<AbseUser> call(BaseRequestResult<List<AbseUser>> result) {
                        return result.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<AbseUser>>(mContext) {
                    @Override
                    public void onNext(List<AbseUser> users) {
                        mMultiItemEntities.clear();
                        mMultiItemEntities.addAll(users);
                        mSignDetailAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void getMothOutUserUsers() {
        mAttendanceApi.getMothOutUserUsers(date, isYear,projectid)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseRequestResult<List<OutUser>>, List<OutUser>>() {
                    @Override
                    public List<OutUser> call(BaseRequestResult<List<OutUser>> result) {
                        return result.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<OutUser>>(mContext) {
                    @Override
                    public void onNext(List<OutUser> users) {
                        mMultiItemEntities.clear();
                        mMultiItemEntities.addAll(users);
                        mSignDetailAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void getMothSecondedUserUsers() {
        mAttendanceApi.getMothSecondedUserUsers(date, isYear,projectid)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseRequestResult<List<SecondedUser>>, List<SecondedUser>>() {
                    @Override
                    public List<SecondedUser> call(BaseRequestResult<List<SecondedUser>> result) {
                        return result.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<SecondedUser>>(mContext) {
                    @Override
                    public void onNext(List<SecondedUser> users) {
                        mMultiItemEntities.clear();
                        mMultiItemEntities.addAll(users);
                        mSignDetailAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void getMothPracticeUserUsers() {
        mAttendanceApi.getMothPracticeUserUsers(date, isYear,projectid)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseRequestResult<List<PracticeUser>>, List<PracticeUser>>() {
                    @Override
                    public List<PracticeUser> call(BaseRequestResult<List<PracticeUser>> result) {
                        return result.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<PracticeUser>>(mContext) {
                    @Override
                    public void onNext(List<PracticeUser> users) {
                        mMultiItemEntities.clear();
                        mMultiItemEntities.addAll(users);
                        mSignDetailAdapter.notifyDataSetChanged();
                    }
                });
    }

}
